var tree;
var deptTreeDialog = new Dialog({
			showdiv : "deptTreeDialog",
			title : "选择部门",
			x : 500,
			y : 500
		});
rootDialog.childDialog = deptTreeDialog;

function initDept(id, type) {
	openDialog(deptTreeDialog, "", function() {
				var divform = deptTreeDialog.contentdiv.appendChild(document
						.createElement("div"));
				var chakantable = document.createElement("div");
				chakantable.id = "Dept_tree_div";
				chakantable.style.textAlign = "left";
				deptTreeDialog.contentdiv.appendChild(chakantable);
				chakantable.innerHTML = "";
				var submitbutton = document.createElement("input");
				submitbutton.setAttribute('type', 'button');
				submitbutton.setAttribute('name', 'submitbutton');
				submitbutton.setAttribute('id', 'submitbutton');
				submitbutton.setAttribute('value', '应用');
				Dept.findDeptTree(function(data) {
							tree = new dhtmlXTreeObject("Dept_tree_div",
									"100%", "100%", 0);
							tree.setImagePath("../../js/imgs/csh_yellowbooks/");
							tree.enableCheckBoxes(1);
							tree.loadJSONObject(data);
							divform.appendChild(submitbutton);
							if (type == "user") {
								submitbutton.onclick = function() {
									var selectv = c(tree.getAllChecked())
											? tree.getAllChecked()
											: "";
									if (selectv && selectv.indexOf(',') == -1) {
										User.addDepts(id, selectv, function(
														data) {
													if (data == 1) {
														alert('为人员选择部门成功');
													} else {
														alert('为人员选择部门失败');
													}
													closeDialog(deptTreeDialog);
												});
									} else {
										// alert('请选择一个部门！');
										if (confirm("确认为此用户选择多个部门吗？")) {
											User.addDepts(id, selectv,
													function(data) {
														if (data == 1) {
															alert('为人员选择部门成功');
														} else {
															alert('为人员选择部门失败');
														}
														closeDialog(deptTreeDialog);
													});
										}
									}
								};
								User.findDeptidByUserUUID(id, function(data) {
											for (var i = 0; i < data.length; i++) {
												tree.setCheck(data[i].deptid,
														true);
												tree.openItem(data[i].deptid);
												tree.closeItem(data[i].deptid);
											}
										});
							} else if (type == "role") {
								submitbutton.onclick = function() {
									var selectv = c(tree.getAllChecked())
											? tree.getAllChecked()
											: "";
									Role.addDepts(id, selectv, function(data) {
												if (data == 1) {
													alert('为部门添加角色成功');
												} else {
													alert('为部门添加角色失败');
												}
												closeDialog(deptTreeDialog);
											});
								};
								Role.findDeptidByRoleid(id, function(data) {
											for (var i = 0; i < data.length; i++) {
												tree.setCheck(data[i].deptid,
														true);
												tree.openItem(data[i].deptid);
												tree.closeItem(data[i].deptid);
											}
										});
							}
						});
			});
}
